{% extends 'base.html' %}
{% from 'bootstrap5/pagination.html' import render_pagination %}

{% block title %}Manage Comments{% endblock %}

{% block content %}
    <div class="page-header">
        <h1>Comments <small class="text-muted">{{ pagination.total }}</small></h1>
        <ul class="nav nav-pills">
            <li class="nav-item">
                <a class="nav-link {% if request.args.get('filter', 'all') == 'all' %}active{% endif %}"
                   href="{{ url_for('admin.manage_comment', filter='all') }}">All</a>
            </li>
            <li class="nav-item">
                <a class="d-flex align-items-center nav-link {% if request.args.get('filter') == 'unread' %}active{% endif %}"
                   href="{{ url_for('admin.manage_comment', filter='unread') }}">
                    {% if unread_comments %}
                        Unread
                        &nbsp;<span class="badge bg-info">{{ unread_comments }}</span>
                    {% endif %}
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if request.args.get('filter') == 'admin' %}active{% endif %}"
                   href="{{ url_for('admin.manage_comment', filter='admin') }}">
                    From Admin
                </a>
            </li>
        </ul>
    </div>
    {% if comments %}
        <table class="table table-secondary table-striped">
            <thead>
            <tr>
                <th>No.</th>
                <th>Author</th>
                <th>Body</th>
                <th>Date</th>
                <th>Actions</th>
            </tr>
            </thead>
            <tbody>
            {% for comment in comments %}
                <tr {% if not comment.reviewed %}class="table-info" {% endif %}>
                    <td>{{ (pagination.page - 1)*config['BLOG_COMMENT_PER_PAGE'] + loop.index }}</td>
                    <td>
                        {% if comment.from_admin %}
                            <a class="badge bg-primary">{{ admin.name }}</a>
                        {% else %}
                            <a class="badge bg-success">{{ comment.author }}</a>
                        {% endif %}
                        <br>
                        {% if comment.site %}
                            <a href="{{ comment.site }}" target="_blank">{{ comment.site }}</a>
                        {% endif %}
                        <br>
                        <a href="mailto:{{ comment.email }}">{{ comment.email }}</a>
                    </td>
                    <td>
                        {{ comment.body }}
                    </td>
                    <td>
                        {{ moment(comment.timestamp).format('LL') }}
                    </td>
                    <td>
                        <form class="inline" method="post"
                              action="{{ url_for('admin.approve_comment', comment_id=comment.id, next=request.full_path) }}">
                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                            <button type="submit" class="btn btn-sm btn-warning" {% if comment.reviewed %}disabled{% endif %}>
                                Approve
                            </button>
                        </form>
                        <a class="btn btn-sm btn-success"
                           href="{{ url_for('blog.show_post', post_id=comment.post.id) }}">
                            Article
                        </a>
                        <form class="inline" method="post"
                              action="{{ url_for('admin.delete_comment', comment_id=comment.id, next=request.full_path) }}">
                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                            <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure?');">
                                Delete
                            </button>
                        </form>
                    </td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
        {{ render_pagination(pagination) }}
    {% else %}
        <div class="card">
            <div class="card-body tip"><h5>There's No Comments.</h5></div>
        </div>
    {% endif %}
{% endblock %}